<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2020/1/4
 * Time: 15:54
 */
namespace app\api\controller;
use think\Db;
class Video extends Base {
     //banner图
    public function banner(){
        $res =  Db::table('up_banner')->where('banner_part',2)->select();
        return json(['status' => 200, 'msg' => $res]);
    }
     //名家名段
    public function person(){
//        $res = Db::table('up_person')
//          ->orderRaw('rand()')->limit(4)->select();
                $res = Db::table('up_person')->order('person_sort asc')
          ->where('person_status',1)
          ->limit(4)->select();
        return json(['status' => 200, 'msg' => $res]);
    }
    //更多名家名段
    public function morePerson(){
        $page = input('param.page')?input('param.page'):1;
        $num = 10;
        $limit = $page-1;
        $res = Db::table('up_school')->limit($limit*$num,$num)->select();
        foreach ($res as $key=>&$vo){
            $list = Db::table('up_person')
                ->where('person_status',1)
                ->where('lp_id',$vo['hot_id'])
                ->select();
            $vo['list'] = $list;
        }
        return json(['status' => 200, 'msg' => $res]);
    }

    //名家详情
    public function detailPerson(){
        extract(input());
        //todo
        $id = input('param.id');
        $page = input('param.page')?input('param.page'):1;
        $type = input('param.type')?input('param.type'):1;//1 视频   2音频    3介绍
        $price = input('param.price');//1免费 2收费 3查全部
        $num = 10;
        $limit = $page-1;
        if($type==1){//视频
            if($price==1){
                $where['a.is_vip'] = 1;
            }elseif ($price==2){
                $where['a.is_vip'] = 2;
            }elseif ($price==3){
                $where = array();
            }
            $ids=','.$id.',';
            $where['a.person_id'] = array('like',"%$ids%");
            $where['is_type'] = 1;
            $res = Db::table('up_video')->alias('a')
                ->join('up_video_modular uvm','uvm.video_modular_id = a.video_f_id','left')
                ->where($where)
                ->limit($limit*$num,$num)
                ->field('a.*,uvm.video_modular_name')
                ->select();
        }elseif ($type==2){//音频
            if($price==1){
                $where['a.is_vip'] = 1;
            }elseif ($price==2){
                $where['a.is_vip'] = 2;
            }elseif ($price==3){
                $where = array();
            }
            $ids=','.$id.',';
            $where['a.person_id'] = array('like',"%$ids%");
            $where['is_type'] = 1;
            $res = Db::table('up_music')->alias('a')
                ->join('up_music_modular umm','umm.video_modular_id = a.music_f_id','left')
                ->where($where)->limit($limit*$num,$num)
                ->field('a.*,umm.video_modular_name')
                ->select();
        }elseif ($type==3){
            $res = Db::table('up_person')->where('person_id','=',$id)->find();
        }
        return json(['status' => 200, 'msg' => $res]);
    }

    public function attention(){
        extract(input());
        //todo
        $id = input('param.id');
        $map['att_person_id'] = $id;
        $map['att_uid'] = $user_id;
        $info = Db::table('up_attention')->where($map)->find();
        if($info['att_id']!=''){
            $is_attention = 1;
        }else{
            $is_attention = 2;
        }
        return json(['status' => 200, 'msg' => $is_attention]);
    }
    //推荐位
    public function recommend(){
        $video_modular_id = Db::table('up_video_modular')->where('is_recommend',1)->select();
        foreach ($video_modular_id as $key=>&$vo){
            $where['video_f_id'] = $vo['video_modular_id'];
            $where['is_type'] = 1;
//            $where['is_vip'] = 1;
            $res = Db::table('up_video')->alias('a')
                ->join('up_video_modular uvm','uvm.video_modular_id = a.video_f_id','left')
                ->limit(4)
                ->where($where)
                ->field('a.*,uvm.video_modular_name')
                ->orderRaw('rand()')
                ->select();
            $vo['list'] = $res;
        }

        return json(['status' => 200, 'msg' => $video_modular_id]);
    }


    //更多推荐位
    public function moreRecommend(){
        $page = input('param.page')?input('param.page'):1;
        $year = input('param.year');//年份
        $school = input('param.school');//流派
        $modular = input('param.modular');//剧种
        $type = input('param.type');//1从低到高   2从高到低
        if($year!=''){
            $where['a.video_year'] = $year;
        }
        if($school!=''){
            $where['a.video_lp_id'] = $school;
        }
        if($modular!=''){
            $where['a.video_f_id'] = $modular;
        }
        if($type!=''){
            if($type==1){
                $order = 'a.video_num asc';
            }elseif($type==2){
                $order = 'a.video_num desc';
            }
        }else{
            $order = 'a.video_num desc';
        }
        $num = 10;
        $limit = $page-1;
//        $video_modular_id = Db::table('up_video_modular')->where('is_recommend',1)->column('video_modular_id');
//        $where['video_f_id'] = array('IN',$video_modular_id);
        $where['is_type'] = 1;
//        $where['is_vip'] = 1;
        $res = Db::table('up_video')->alias('a')
            ->join('up_video_modular uvm','uvm.video_modular_id = a.video_f_id','left')
            ->limit($limit*$num,$num)
            ->where($where)
            ->field('a.*,uvm.video_modular_name')
            ->order($order)
            ->select();
        return json(['status' => 200, 'msg' => $res]);
    }
    //流派
    public function school(){
        $res = Db::table('up_school')->select();
        return json(['status' => 200, 'msg' => $res]);
    }
    //剧种
    public function modular(){
        $res = Db::table('up_video_modular')->field('video_modular_id,video_modular_name')->select();
        return json(['status' => 200, 'msg' => $res]);
    }
    //年份
    public function year(){
        $res = Db::table('up_video')->group('video_year')->field('video_year')->select();
        return json(['status' => 200, 'msg' => $res]);
    }

    //视频详情
    public function detailVideo(){
        extract(input());
        $id = input('param.id');
        $res = Db::table('up_video')->alias('a')
            ->join('up_video_modular uvm','uvm.video_modular_id = a.video_f_id','left')
            ->where('video_id',$id)
            ->field('a.*,uvm.video_modular_name')
            ->find();
        $result = Db::table('up_video_chirden')->where('f_id',$res['video_id'])->select();
        $res['chirden'] = $result;
        //查询是否收藏
        $where['user_id'] = $user_id;
        $where['is_type'] = 1;
        $where['f_id'] = $res['video_id'];
        $info = Db::table('up_like')->where($where)->find();
        if($info['like_id']!=''){
            $res['is_like'] = 1;
        }else{
            $res['is_like'] = 2;
        }
        //查询评论
        $where1['type'] = 1;
        $where1['f_id'] = $res['video_id'];
        $num = Db::table('up_comment')->where($where1)->count();
        $res['comment_num'] = $num;
        //查询这个用户的观看历史记录
        $whereMap['user_id'] = $user_id;
        $whereMap['media_type'] = 1;
        $whereMap['media_id'] = $id;
        $info1 = Db::table('up_history')->where($whereMap)->order('create_time desc')->find();
        if($info1['histroy_id']==''){//证明没有历史记录
            $res['is_history'] = 1;
        }else{//有历史记录
           $res['is_history'] =2;
           $res['histroy_time'] =$info1['histroy_time'];
           $res['children_id'] =$info1['children_id'];
        }
        return json(['status' => 200, 'msg' => $res]);
    }
     //评论详情
    public function detailComment(){
        $page = input('param.page')?input('param.page'):1;
        $num = 10;
        $limit = $page-1;
        $id = input('param.id');
        $res = Db::table('up_comment')->alias('a')
            ->join('up_u_user uuu','uuu.user_id = a.user_id','left')
            ->field('uuu.user_username,uuu.user_image,a.*')
            ->where('a.f_id',$id)
            ->where('type',1)
            ->limit($limit*$num,$num)
            ->select();

        return json(['status' => 200, 'msg' => $res]);
    }

    //视频广告
    public function poster(){
        $res = Db::table('up_poster')
            ->limit(1)
            ->orderRaw('rand()')
            ->find();
        return json(['status' => 200, 'msg' => $res]);
    }

    /***
     * @return \think\response\Json
     * 基本信息
     */
    public function user(){
        extract(input());
        $res = Db::table('up_u_user')
            ->field('is_vip,end_time,user_image')
            ->where('user_id',$user_id)
            ->find();
        return json(['status' => 200, 'msg' => $res]);
    }

    /***
     * 记录用户浏览视频的接口
     */
    public function record(){
        extract(input());
        $video_id = input('param.video_id');
        $where['user_id'] = $user_id;
        $where['video_id'] = $video_id;
        $res = Db::table('up_record_video')->where($where)->find();
        if($res['record_video_id']!=''){//查询到这个用户有这个视频的观看记录
            Db::table('up_record_video')->where('record_video_id',$res['record_video_id'])->setInc('video_num');
        }else{
            Db::table('up_record_video')->insert($where);
        }
        Db::table('up_video')->where('video_id',$video_id)->setInc('video_num');
        return json(['status' => 200, 'msg' => "成功"]);
    }

    /**
     * @return \think\response\Json
     * 退出观看历史
     */
    public function loginOut(){
        extract(input());
        $video_id = input('param.video_id');
        $where['user_id'] = $user_id;
        $where['video_id'] = $video_id;
        $res = Db::table('up_record_video')->where($where)->order('record_video_id desc')->find();
        $time =date('Y-m-d H:i:s',time());
        Db::table('up_record_video')->where('record_video_id',$res['record_video_id'])->setField('video_time',$time);
        return json(['status' => 200, 'msg' => $res]);

    }
}